#include "newgamedialog.h"
#include "ui_newgamedialog.h"

#include <QtNetwork>

NewGameDialog::NewGameDialog(QWidget *parent) :
   QDialog(parent),
   ui(new Ui::NewGameDialog)
{
   ui->setupUi(this);
   isHost = false;

   QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();

   // Add ip4 addresses to listbox
   for (int i = 0; i < ipAddressesList.size(); ++i)
   {
      if (ipAddressesList.at(i).toIPv4Address())
      {
         QListWidgetItem *item = new QListWidgetItem(ipAddressesList.at(i).toString());
         ui->interfaceList->addItem(item);
         if (item->text() == "127.0.0.1")
            ui->interfaceList->setCurrentItem(item);
      }
   }

}

NewGameDialog::~NewGameDialog()
{
   delete ui;
}

void NewGameDialog::onHostButton()
{
   isHost = true;
   copyFields();
}

void NewGameDialog::onJoinButton()
{
   copyFields();
}

void NewGameDialog::onItemPressed(QListWidgetItem *item)
{
   hostIP = item->text();
}


void NewGameDialog::copyFields()
{
   hostIP = ui->interfaceList->currentItem()->text();
   hostPort = ui->hostPort->text().toInt();
   joinIP = ui->joinIP->text();
   joinPort = ui->joinPort->text().toInt();
}
